package com.wzg.appframe.web.jsbridge;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.wzg.appframe.R;
import com.wzg.appframe.utils.TLog;
import com.wzg.appframe.web.ProgressWebView;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * js和android通信桥梁WebView，封装好js和安卓通信机制
 * Created by youliang.ji on 2016/12/23.
 */

public class BridgeWebView extends WebView {

    /***
     * js调用android方法的映射字符串
     **/
    private static final String JS_INTERFACE = "nh";

    private ProgressBar mProgressBar;

    public BridgeWebView(Context context) {
        super(context);
        initProgressBar();
    }

    public BridgeWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initProgressBar();
    }

    public BridgeWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initProgressBar();
    }

    void initProgressBar() {
        mProgressBar = new ProgressBar(getContext(), null,
                android.R.attr.progressBarStyleHorizontal);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, 8);
        mProgressBar.setLayoutParams(layoutParams);

        Drawable drawable = getContext().getDrawable(
                R.drawable.web_progress_bar_states);
        mProgressBar.setProgressDrawable(drawable);
//        addView(mProgressBar);
        setWebChromeClient(new BridgeWebView.WebChromeClient());
    }

    public class WebChromeClient extends android.webkit.WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
                mProgressBar.setVisibility(GONE);
            } else {
                if (mProgressBar.getVisibility() == GONE)
                    mProgressBar.setVisibility(VISIBLE);
                mProgressBar.setProgress(newProgress);
            }
            super.onProgressChanged(view, newProgress);
        }
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
//        LayoutParams lp = (LayoutParams) mProgressBar.getLayoutParams();
//        lp.x = l;
//        lp.y = t;
//        mProgressBar.setLayoutParams(lp);
        super.onScrollChanged(l, t, oldl, oldt);
    }

    /**
     * 注册js和android通信桥梁对象
     *
     * @param obj 桥梁类对象,该对象提供方法让js调用,默认开启JavaScriptEnabled=true
     */
    public void addBridgeInterface(Object obj) {
        this.getSettings().setJavaScriptEnabled(true);
        addJavascriptInterface(new MyJavaScriptMethod(obj), JS_INTERFACE);
    }

    /**
     * 注册js和android通信桥梁对象
     *
     * @param obj 桥梁类对象,该对象提供方法让js调用
     * @param url 默认开启JavaScriptEnabled=true
     */
    public void addBridgeInterface(Object obj, String url) {
        this.getSettings().setJavaScriptEnabled(true);
        addJavascriptInterface(new MyJavaScriptMethod(obj), JS_INTERFACE);
        loadUrl(url);
    }

    /**
     * 回调js方法
     *
     * @param json 参数，json格式字符串
     */
    public void callbackJavaScript(String json) {

    }

    private void invokeJavaScript(String callback, String params) {

    }

    /**
     * 内置js桥梁类
     * Created by youliang.ji on 2016/12/23.
     */

    public class MyJavaScriptMethod {

        private Object mTarget;
        private Method targetMethod;


        public MyJavaScriptMethod(Object targer) {
            this.mTarget = targer;
        }

        /**
         * 内置桥梁方法
         *
         * @param method 方法名
         * @param json   js传递参数，json格式
         */
        @JavascriptInterface
        public void invokeMethod(String method, String[] json) {
            Class<?>[] params = new Class[]{String[].class};
            TLog.d("invokeMethod=" + method + "#json.length" + json.length);
            try {
                Method targetMethod = this.mTarget.getClass().getDeclaredMethod(method, params);
                targetMethod.invoke(mTarget, new Object[]{json});//反射调用js传递过来的方法，传参

            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

        }
    }

}
